package cj.web.admin.controllers;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.dao.ColumnsDAO;
import cj.web.admin.dao.ColumnsRefDAO;
import cj.web.admin.dao.RoleInfoDAO;
import cj.web.admin.domain.Columns;
import cj.web.admin.domain.ColumnsRef;
import cj.web.admin.domain.RoleInfo;
import cj.web.constants.ColumnsStatusEnum;
import cj.web.constants.ColumnsSubTypeEnum;
import cj.web.constants.ColumnsTypeEnum;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

/**
 * 栏目
 * @author Administrator
 *
 */
@Path("/columns")
public class ColumnsController extends BaseController{

	private static Logger logger = Logger.getLogger(ColumnsController.class.getName());
	
	@Autowired
	private ColumnsDAO columnsDao;
	@Autowired
	private RoleInfoDAO roleInfoDao;
	@Autowired
	private ColumnsRefDAO columnsRefDAO;
	
	@Get("show")
	public String show(Model model,Columns columns) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkglmgl);
        model.add("type",ColumnsTypeEnum.LM.getValue());
        model.add("ColumnsEnum",EnumSet.allOf(ColumnsTypeEnum.class));
        model.add("ColumnsStatusEnum",EnumSet.allOf(ColumnsStatusEnum.class));
		model.add("ColumnsSubTypeEnum",EnumSet.allOf(ColumnsSubTypeEnum.class));
		return "/views/news/columnsList.vm";
	}

	@Post("getList")
	public String queryList(Columns columns) {
	
		List<Columns> arr = columnsDao.query(columns);
		
		for(Columns obj:arr)
		{
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
		}

		JSONObject jo = new JSONObject();

		int size = columnsDao.querySize(columns);

		jo.put("data", arr);
		jo.put("draw", columns.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkglmgl);
        model.add("type",ColumnsTypeEnum.LM.getValue());
        model.add("ColumnsSubTypeEnum",EnumSet.allOf(ColumnsSubTypeEnum.class));
		return "/views/news/columnsDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Columns columns,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			columns.setStatus(ColumnsStatusEnum.FB.getValue());
			columns.setCreateUser(returnSessionObj(request).getAccount());
			columns.setUpdateUser(returnSessionObj(request).getAccount());
			columnsDao.create(columns);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id)
	{
		Columns obj = columnsDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkglmgl);
        model.add("type",ColumnsTypeEnum.LM.getValue());
        model.add("ColumnsSubTypeEnum",EnumSet.allOf(ColumnsSubTypeEnum.class));
		return "/views/news/columnsEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Columns columns,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			columns.setUpdateUser(returnSessionObj(request).getAccount());
			columnsDao.update(columns);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		Columns obj = columnsDao.queryById(id);
		
		if(obj!=null)
		{
			columnsDao.deleteById(id);
		}
		return "r:show";
	}
	
	//置顶
	@Get("stick")
    public String stick(@Param("id") int id) {
		
		Columns obj = columnsDao.queryById(id);
		
		if(obj!=null)
		{
			columnsDao.updateByStickTime(id);
		}
		
		return "r:show";
	}
	
	//取消置顶
	@Get("channelStick")
    public String channelStick(@Param("id") int id) {
		
		Columns obj = columnsDao.queryById(id);
		
		if(obj!=null)
		{
			columnsDao.updateByStickTimeIsNull(id);
		}
		
		return "r:show";
	}
	
	//发布
	@Get("updateByStatusFb")
    public String updateByStatusFb(@Param("id") int id) {
		
		Columns obj = columnsDao.queryById(id);
		
		if(obj!=null)
		{
			columnsDao.updateByStatusFb(id);
		}
		
		return "r:show";
	}
	
	//未发布
	@Get("updateByStatusWfb")
    public String updateByStatusWfb(@Param("id") int id) {
		
		Columns obj = columnsDao.queryById(id);
		
		if(obj!=null)
		{
			columnsDao.updateByStatusWfb(id);
		}
		
		return "r:show";
	}	
	
	/**
	 * 栏目权限列表页面
	 * @param model
	 * @return
	 */
	@Get("showRoleList")
	public String showRoleList(Model model) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkglmqx);
        model.add("type",ColumnsTypeEnum.LM.getValue());
        model.add("ColumnsEnum",EnumSet.allOf(ColumnsTypeEnum.class));
        model.add("ColumnsStatusEnum",EnumSet.allOf(ColumnsStatusEnum.class));
		model.add("ColumnsSubTypeEnum",EnumSet.allOf(ColumnsSubTypeEnum.class));
		return "/views/news/columnsRoleList.vm";
	}

	/**
	 * 查询栏目Json
	 * @param columns
	 * @return
	 */
	@Post("getRoleList")
	public String queryRoleList(Columns columns) {
	
		List<Columns> arr = columnsDao.query(columns);
		
		for(Columns obj:arr)
		{
			List<Map> repColumns = columnsDao.queryColums(obj.getId(), ColumnsTypeEnum.LM.getValue());
			
			obj.setRoleName(repColumns.get(0).get("roleName")==null?"":repColumns.get(0).get("roleName").toString());
		}
		
		JSONObject jo = new JSONObject();

		int size = columnsDao.querySize(columns);
		jo.put("data", arr);
		jo.put("draw", columns.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();	
	}
	
	/**
	 * 栏目权限配置页面
	 * @param model
	 * @param id
	 * @return
	 */
	@Get("showColumnsRole")
	public String showColumnsRole(Model model,@Param("id") int id) {
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkglmqx);
        Columns obj = columnsDao.queryById(id);
		
		if(obj!=null){
			model.add("obj",obj);
			String roleIds =columnsRefDAO.queryRoleByColumnsId(id);
			model.add("roleIds",roleIds);
		}
		return "/views/news/columnsRole.vm";
	}
	
	/**
	 * 栏目角色树形结构
	 * @param id
	 * @return
	 */
	@Get("columnsRefRoleJson")
	public String columnsRefRoleJson(@Param("id") int id)
	{
		String roleId =columnsRefDAO.queryRoleByColumnsId(id);
		JSONObject jo = new JSONObject();
		List<RoleInfo> roleInfos = roleInfoDao.getListAll();
		List<Map> reps = new ArrayList<Map>();
		for(RoleInfo obj:roleInfos)
		{
			Map rep = new HashMap();
			rep.put("id", obj.getId());
			rep.put("pId", 0);
			rep.put("name", obj.getDescs());
			rep.put("open", true);
			if(roleId!=null)
			{
				rep.put("checked", (","+roleId+",").indexOf(","+obj.getId()+",")!=-1?true:false);
			}
			else
			{
				rep.put("checked", false);
			}
			
			reps.add(rep);
		}
		jo.put("reps", reps);
		return "@"+jo.toString();
	}
	
	/**
	 * 修改栏目权限
	 * @param columnsId
	 * @param roleIds
	 * @return
	 */
	@Post("updateRoleColumnsRef")
	public String updateRoleColumnsRef(@Param("columnsId")int columnsId, @Param("roleIds")String roleIds) {
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			columnsRefDAO.deleteById(columnsId);
			
			createRoleColumnsRef(roleIds,columnsId);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo;
	}
	
	private void createRoleColumnsRef(String roleIds, int columnsId) {
		if (roleIds != null && roleIds.length() > 0) {
			for (String roleId : roleIds.split(",")) {
				ColumnsRef obj = new ColumnsRef();

				obj.setColumnsId(columnsId);
				obj.setRoleId(Integer.parseInt(roleId));

				columnsRefDAO.create(obj);
			}
		}
		
	}
}
